package com.itheima.core.web.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.core.po.RegisterFormBean;
import com.itheima.core.po.User;
import com.itheima.core.service.UserService;
/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode,String password, Model model, 
                                                          HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(usercode, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
            // 将所有用户对象添加到Session
            List<User> sys_users = userService.selectAllUser();
            session.setAttribute("sys_users", sys_users);
			// 跳转到主页面
            //return "customer";
			return "redirect:customer/list.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	
	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
	    return "customer";
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "login";
	}
	
	/**
	 * 查询用户是否存在
	 */
	@RequestMapping(value = "/user/userIsExist.action", method = RequestMethod.POST)
    @ResponseBody
    public Object userIsExist(@RequestBody Map<String,String> requestMap) {
        System.out.println(requestMap);
        Map<String, Object> map = new HashMap<>();
        boolean userIsExist = userService.findUserIsExist(requestMap.get("user_code"));
        if (userIsExist) {
            map.put("result", true);
        } else {
            map.put("result", false);
        }
        return map;
    }
    
    /**
     * 用户注册
     */
    @RequestMapping(value = "/user/register.action", method = RequestMethod.POST)
    @ResponseBody
    public Object register(@RequestBody RegisterFormBean registerFormBean) {
        System.out.println(registerFormBean);
        Map<String, Object> map = new HashMap<>();
        boolean validate = userService.register(registerFormBean);
        if (validate) {
            map.put("result", true);
            map.put("msg", "注册成功");
            return map;
        }else {
            return registerFormBean.getMsg();
        }
    }
    
    /**
     * 删除用户
     */
    @RequestMapping(value = "/user/delete.action", method = RequestMethod.POST)
    @ResponseBody
    public Object deleteUser(@RequestBody Map<String,String> requestMap) {
        Map<String, Object> map = new HashMap<>();
        User user=new User();
        user.setUser_code(requestMap.get("user_code"));
        boolean b = userService.deleteUser(user);
        if (b) {
            map.put("result", true);
        } else {
            map.put("result", false);
        }
        return map;
    }
    
    /**
     * 更新用户信息
     */
    @RequestMapping(value = "/user/update.action", method = RequestMethod.POST)
    @ResponseBody
    public Object updateUser(@RequestBody Map<String,Object> requestMap,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        User user=new User();
        user.setUser_code((String) requestMap.get("user_code"));
        user.setUser_name((String) requestMap.get("user_name"));
        user.setUser_state(Integer.parseInt((String) requestMap.get("user_state")));
        user.setUser_password((String) requestMap.get("user_password"));
        boolean b = userService.updateUser(user);
        if (b) {
            System.out.println("old-user:"+session.getAttribute("USER_SESSION"));
            User user1= (User) session.getAttribute("USER_SESSION");
            user1.setUser_name((String) requestMap.get("user_name"));
            user1.setUser_state(Integer.parseInt((String) requestMap.get("user_state")));
            session.setAttribute("USER_SESSION", user1);
            System.out.println("new-user:"+session.getAttribute("USER_SESSION"));
            map.put("result", true);
        } else {
            map.put("result", false);
        }
        return map;
    }
    
    /**
     * 批量删除用户
     */
    @RequestMapping(value = "/user/deletePatch.action", method = RequestMethod.POST)
    @ResponseBody
    public Object deletePatch(@RequestBody List<String> requestList,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(requestList);
        Integer count = userService.deletePatch(requestList);
        if (count > 0) {
            // 将所有用户对象添加到Session
            List<User> sys_users = userService.selectAllUser();
            session.setAttribute("sys_users", sys_users);
            map.put("result", true);
        } else {
            map.put("result", false);
        }
        return map;
    }
    
}
